package com.lc.luzhengtang.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.AuthTask;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.ConfirmOrderActivity;
import com.lc.luzhengtang.activity.EvaluateActivity;
import com.lc.luzhengtang.activity.OrderDetailActivity2;
import com.lc.luzhengtang.activity.RefundActivity;
import com.lc.luzhengtang.adapter.SectionOrderListAdapter;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.OrderListBean;
import com.lc.luzhengtang.conn.ConnCancelOrder;
import com.lc.luzhengtang.conn.ConnOrderList;
import com.lc.luzhengtang.conn.ConnPay;
import com.lc.luzhengtang.conn.ConnSubmitShouhuo;
import com.lc.luzhengtang.divider.SimpleDividerItemDecoration;
import com.lc.luzhengtang.interfaces.NoticeActivity;
import com.lc.luzhengtang.util.AuthResult;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.util.OrderInfoUtil2_0;
import com.lc.luzhengtang.util.PayResult;
import com.lc.luzhengtang.util.WxPay;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


@SuppressLint("ValidFragment")
public class SimpleOrderFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, SectionOrderListAdapter.OnCommentClickListener {
    @BindView(R.id.card_title_tv)
    TextView cardTitleTv;
    Unbinder unbinder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private String mTitle;
    private static final String TAG = "SimpleOrderFragment";
    SectionOrderListAdapter sectionOrderListAdapter;
    private int currentPage = 1;
    List<OrderListBean.DataBean> results;
    List<OrderListBean.DataBean.OrderslistBean> money_list = new ArrayList<>();
    private String ordersn;
    private PopupWindow popWindow, popWindow2;

    private PopupWindow popWindow3;
    public static final String RSA_PRIVATE = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJVxm7ggLNGRIY3Yt3xoZjrwMzTBCYqM/6X0MiHTds0Ba81dP7LnEBG8tBnc9NV2zWPCOXdPnrN7iGuWxC70C0fhbRPARDRUrbUZqExIeF6UDr6SumGIZZsLoQYobtZxfMXQAQaSgB6/ejbwqY1saHwo7AbG3nMXT7N8m+4p4V0ZAgMBAAECgYBtms88scxtP2Y+pI2eXutrmrPToyieR19QldMW4AMnxD0Od6+6RKGSgLYOqfGfheyJl++RxAF/XcDmN8NXNIH4ZovJLaxLGh44bG9d2MGAApBFbWLM4ZiJ9uK5KyIYztDuYkEicBkHGSe6YtNMe3lX3cTMPMPpHTyODRWitQI7SQJBAPK5eFQf8CXWRZtvv7cwdRjwY4/SGh4Fh25ZwUeizn4/EnZl/XMmYnLVdhQn6sFo5YxC4FJD3vRD0pvXu5lrUNsCQQCdng8+plLI4k7v3lB32Oyv1Xwu/q3CDh9QN9AMKk/5aPD/YrQ9mC6VfQNb5UR30P0/t+SAIOa5/3j9opH4lGIbAkEAjxnF1WMXBFe1phRWJTyObOuc5zfpafW8iNSl3QQhVdD9o4wxxufEdwtemDeIcgrT+sOa2SVmlwA9dN/VnHfmrQJAU+JuHJqqTm1bM3/u9YnGleIZoG08jp2zfp7lo8PVIlj+gfzHDkzy5e5NnlfLc7UPQx0ciDB6ex0DCIHADDr0ZwJBAO4ooeZitqD10l6p7pgP2qz+BYyug+/wJHNpZ2dF12Jp8m6KpD46XK6AvzyOT3HXEoQOmtMABBPktUfEHBELR/A=";
    public static final String APPID = "2017051007189568";
    public static final String RSA2_PRIVATE = "";
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    String money;
    String getMoney;
    String order_id;
    ArrayList<Integer> idList;


    public static SimpleOrderFragment getInstance(String title) {
        SimpleOrderFragment sf = new SimpleOrderFragment();
        sf.mTitle = title;
        return sf;
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        popWindow.dismiss();
                        ((NoticeActivity<Integer>) getActivity()).onRefresh(1);
                        onRefresh();
//                        onRefresh();
                        popWindow = null;
                    } else {
                        ((NoticeActivity<Integer>) getActivity()).onRefresh(0);
                        popWindow.dismiss();
                        money = "";
                        popWindow = null;
                        onRefresh();
                    }
                    break;
                }
                case SDK_AUTH_FLAG: {
                    @SuppressWarnings("unchecked")
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    String resultStatus = authResult.getResultStatus();

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        Toast.makeText(getActivity(),
                                "授权成功\n" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT)
                                .show();
                    } else {
                        // 其他状态值则为授权失败
                        Toast.makeText(getActivity(),
                                "授权失败" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT).show();

                    }
                    break;

                }
                default:
                    break;
            }
        }

        ;
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fr_simple_order, null);


        unbinder = ButterKnife.bind(this, v);
        cardTitleTv.setText(mTitle);
//        Toast.makeText(getActivity(), mTitle, Toast.LENGTH_SHORT).show();

        return v;
    }


    String getOrdersn;
    String getalipayOrder;

    protected void initData() {
        paytype = "2";
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        sectionOrderListAdapter = new SectionOrderListAdapter(null);
        sectionOrderListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        sectionOrderListAdapter.setOnCommentClickListener(this);

        sectionOrderListAdapter.isFirstOnly(false);

        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 1));

        recyclerView.setAdapter(sectionOrderListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        sectionOrderListAdapter.setOnLoadMoreListener(this);
        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
        sectionOrderListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                ordersn = results.get(position).getOrdersn();
                order_id = results.get(position).getId() + "";
                switch (view.getId()) {
                    case R.id.btn_cancelorder:
                        showPopupWindow2(view);
                        break;
                    case R.id.btn_submit_shouhuo:
                        showPopupWindow3(view);
                        break;
                    case R.id.btn_pay:
//                        money = results.get(position).getOrderslist().get(position).getGoods_price();
//                        money = money_list.get(position).getGoods_price();
                        getalipayOrder = results.get(position).getOrdersn();
                        Log.d(getalipayOrder, "444444444444444444444444444");
                        Log.d(getalipayOrder, "444444444444444444444444444");
                        money = results.get(position).getTotal();
                        getMoney = results.get(position).getTotal();
                        showPopupWindow(view);
                        break;

                    //去退款界面
                    case R.id.btn_tuikuan:
//                        startActivity(new Intent(getActivity(), RefundActivity.class));
                        Intent intent = new Intent();
                        intent.putExtra("ordersn", ordersn);
//                        intent.putExtra("mTitle",mTitle);
                        intent.setClass(getActivity(), RefundActivity.class);

                        startActivity(intent);

                        break;

                    case R.id.btn_pay_detail:

                        Intent intent2 = new Intent();
                        intent2.putExtra("ordersn", results.get(position).getOrdersn());
                        intent2.putExtra("status", results.get(position).getStatus() + "");
                        intent2.putExtra("order_id", results.get(position).getId() + "");
                        intent2.putExtra("weburl", results.get(position).getUrl());
                        intent2.putIntegerArrayListExtra("stringList", (ArrayList<Integer>) idList);
                        intent2.setClass(getActivity(), OrderDetailActivity2.class);
                        startActivity(intent2);
                        break;
//取消订单的

                    case R.id.text_cancel_order:
//                        showPopupWindow2(view);
                        Intent intent3 = new Intent();
                        intent3.putExtra("ordersn", results.get(position).getOrdersn());
                        intent3.putExtra("status", results.get(position).getStatus() + "");
                        intent3.putExtra("order_id", results.get(position).getId() + "");
                        intent3.putExtra("weburl", results.get(position).getUrl());
                        intent3.putExtra("getpaytype",results.get(position).getPaytype());

                        intent3.putIntegerArrayListExtra("stringList", (ArrayList<Integer>) idList);
                        intent3.setClass(getActivity(), OrderDetailActivity2.class);
                        startActivity(intent3);
                        break;


                }


            }
        });
    }


    private void showPopupWindow3(View parent) {
        if (popWindow3 == null) {
    /*        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_01, null)));*/
//            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
            View view = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_01, null));

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow3 = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop3(view);
        }
        //设置动画效果
        popWindow3.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow3.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow3.setOutsideTouchable(true);
        popWindow3.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow3.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow3.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void initPop3(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册

        TextView text_cadncel = (TextView) view.findViewById(R.id.text_cadncel);
        text_cadncel.setText("是否确认收货");
        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow3.dismiss();

            }

        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow3.dismiss();
                new ConnSubmitShouhuo(ordersn, new AsyCallBack<CommenBean>() {

                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
//                                Toast.makeText(getActivity(), "确认收货成功", Toast.LENGTH_SHORT).show();
                        LovelyToast.makeText(getActivity(), "确认收货成功", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);

                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                        initData();
                    }
                }

                ).execute(getActivity(), false);


                popWindow3.dismiss();
                onRefresh();

            }
        });
    }


    private void showPopupWindow(View parent) {
        if (popWindow == null) {
    /*        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_01, null)));*/
//            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
            View view = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_fukuan, null));

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    private String paytype = "2";

    public void initPop(View view) {
        LinearLayout layout_dismiss = (LinearLayout) view.findViewById(R.id.layout_dismiss);
        layout_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                money = "";
                popWindow = null;
            }
        });
        LinearLayout layout_wx = (LinearLayout) view.findViewById(R.id.layout_wx);
        final ImageView img_wx = (ImageView) view.findViewById(R.id.img_wx);

        LinearLayout layout_alipay = (LinearLayout) view.findViewById(R.id.layout_alipay);
        final ImageView img_alipay = (ImageView) view.findViewById(R.id.img_alipay);
        LinearLayout layout_yue = (LinearLayout) view.findViewById(R.id.layout_yue);
        final ImageView img_yue = (ImageView) view.findViewById(R.id.img_yue);
        LinearLayout layout_xianxia = (LinearLayout) view.findViewById(R.id.layout_xianxia);
        final ImageView img_offline = (ImageView) view.findViewById(R.id.img_offline);
        layout_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_wx.setImageResource(R.mipmap.checkboxselected);
                img_alipay.setImageResource(R.mipmap.checkboxunselected);
                img_yue.setImageResource(R.mipmap.checkboxunselected);
                img_offline.setImageResource(R.mipmap.checkboxunselected);
//                popWindow.dismiss();
                paytype = "2";
            }
        });
        layout_alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_wx.setImageResource(R.mipmap.checkboxunselected);
                img_alipay.setImageResource(R.mipmap.checkboxselected);
                img_yue.setImageResource(R.mipmap.checkboxunselected);
                img_offline.setImageResource(R.mipmap.checkboxunselected);
//                popWindow.dismiss();
                paytype = "3";
            }
        });

        layout_yue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_wx.setImageResource(R.mipmap.checkboxunselected);
                img_alipay.setImageResource(R.mipmap.checkboxunselected);
                img_yue.setImageResource(R.mipmap.checkboxselected);
                img_offline.setImageResource(R.mipmap.checkboxunselected);
                paytype = "1";
//                popWindow.dismiss();
            }
        });
        layout_xianxia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_wx.setImageResource(R.mipmap.checkboxunselected);
                img_alipay.setImageResource(R.mipmap.checkboxunselected);
                img_yue.setImageResource(R.mipmap.checkboxunselected);
                img_offline.setImageResource(R.mipmap.checkboxselected);
//                popWindow.dismiss();
                paytype = "4";
            }
        });


//        textBalance.setText("（" + "¥" + myBanance + "）");
        TextView text_balance = (TextView) view.findViewById(R.id.text_balance);
        text_balance.setText("（" + "¥" + BaseApplication.basePreferences.readBalance() + "）");

        TextView text_allmoney = (TextView) view.findViewById(R.id.text_allmoney);
        text_allmoney.setText("￥" + money);
        Button submit_order = (Button) view.findViewById(R.id.submit_order);


        submit_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(paytype)) {
                    LovelyToast.makeText(getActivity(), "请选择支付方式", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (paytype.equals("1") && Double.valueOf(getMoney)<=Double.valueOf(BaseApplication.basePreferences.readBalance()).intValue()) {
                    goPay();
                }else if (paytype.equals("1") && Double.valueOf(getMoney)>Double.valueOf(BaseApplication.basePreferences.readBalance()).intValue()){
                    LovelyToast.makeText(getActivity(), "余额不足请选择其他支付方式", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }



                else if (paytype.equals("4")) {
                    goPay();

                } else if (paytype.equals("3")) {
                    alipayData();
                } else {
                    WxPay wxPay = new WxPay(getActivity());


                    try {
                        double money = Double.parseDouble(getMoney) * 100;
                        wxPay.pay("清君堂", ordersn, subZeroAndDot(String.valueOf(money)), "3");
                        popWindow.dismiss();
                        popWindow = null;
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(), "数据加载失败", Toast.LENGTH_SHORT).show();
                        popWindow.dismiss();
                        popWindow = null;
                    }


                }
//                goPay();
            }
        });
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return
     */
    private String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    public void goPay() {

        new ConnPay(ordersn, paytype, new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                if (commenBean.getCode() == 200) {
                    popWindow.dismiss();
                    money = "";
                    ((NoticeActivity<Integer>) getActivity()).onRefresh(1);
                    onRefresh();
                    LovelyToast.makeText(getActivity(), commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    LovelyToast.makeText(getActivity(), commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }
                initData();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }

        }


        ).execute(getActivity(), false);
    }


    public void goPay2() {

        new ConnPay(ordersn, paytype, new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                if (commenBean.getCode() == 200) {

//                    money = "";
//                    ((NoticeActivity<Integer>) getActivity()).onRefresh(1);

                    LovelyToast.makeText(getActivity(), commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    onRefresh();

                } else {
                    LovelyToast.makeText(getActivity(), commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }
//                initData();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }

        }


        ).execute(getActivity(), false);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void getOrderData() {

    }

    @Override
    public void onRefresh() {
        paytype = "2";
        currentPage = 1;
        if (popWindow != null) {
            popWindow.dismiss();
            money = "";

        }
        new ConnOrderList(BaseApplication.basePreferences.readUid(), mTitle, new AsyCallBack<OrderListBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
//                showLoading();
                sectionOrderListAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, final OrderListBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                results = s.getData();
                if (results != null) {
                    currentPage = 1;
                    sectionOrderListAdapter.setNewData(results);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                sectionOrderListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
//                dismissLoading();
                sectionOrderListAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(getActivity(), false);
    }

    @Override
    public void onLoadMoreRequested() {

    }

    private String mSum;

    public void alipayData() {

        /**
         * 这里只是为了方便直接向商户展示支付宝的整个支付流程；所以Demo中加签过程直接放在客户端完成；
         * 真实App里，privateKey等数据严禁放在客户端，加签过程务必要放在服务端完成；
         * 防止商户私密数据泄露，造成不必要的资金损失，及面临各种安全风险；
         *
         * orderInfo的获取必须来自服务端；
         */

        String name = "鹿正堂";

        String body = getalipayOrder + "," + "0" + "," + "1" + "," + BaseApplication.basePreferences.readUid();
//        String body = BaseApplication.basePreferences.readUid();
        boolean rsa2 = false;
//        Map<String, String> authInfoMap = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2, body, ordersn, num_shop, name);
        Map<String, String> authInfoMap = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2, body, getalipayOrder, getMoney, name);
        String info = OrderInfoUtil2_0.buildOrderParam(authInfoMap);

        String sign = OrderInfoUtil2_0.getSign(authInfoMap, ConfirmOrderActivity.RSA_PRIVATE, rsa2);
        final String authInfo = info + "&" + sign;
        Log.i("123456789", authInfo);
        Runnable authRunable = new Runnable() {
            @Override
            public void run() {
                // 构造PayTask对象
                AuthTask authTask = new AuthTask(getActivity());
                // 调用支付接口,获取支付结果
                Map<String, String> result = authTask.authV2(authInfo, true);
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };
        // 必须异步调用
        Thread authThread = new Thread(authRunable);
        authThread.start();
    }

    String number;

    private void showPopupWindow2(View parent) {
        if (popWindow2 == null) {
    /*        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_01, null)));*/
//            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
            View view2 = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_01, null));

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view2);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop2(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册

        TextView text_cadncel = (TextView) view.findViewById(R.id.text_cadncel);
        text_cadncel.setText("是否取消订单");
        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                popWindow2.dismiss();

            }

        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow2.dismiss();

                new ConnCancelOrder(order_id, new AsyCallBack<CommenBean>() {
                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
                        LovelyToast.makeText(getActivity(), commenBean.getMessage(), 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                    }
                }


                ).execute(getActivity(), false);

                popWindow2.dismiss();
                onRefresh();

            }
        });
    }


    @Override
    public void onCommentClick(OrderListBean.DataBean dataBean) {
        idList = new ArrayList<>();
        final ArrayList<String> picList = new ArrayList<>();
        List<OrderListBean.DataBean.OrderslistBean> orderslist = dataBean.getOrderslist();
        for (OrderListBean.DataBean.OrderslistBean bean : orderslist) {
            idList.add(bean.getOrderslist_id());
            picList.add(bean.getPicurl());
        }
        Intent intent = new Intent(getActivity(), EvaluateActivity.class);
        Bundle bundle = new Bundle();
        bundle.putIntegerArrayList(EvaluateActivity.KEY_IDS, idList);
        bundle.putStringArrayList(EvaluateActivity.KEY_PICS, picList);
        bundle.putString(EvaluateActivity.KEY_ORDERSN, dataBean.getOrdersn());
        intent.putExtras(bundle);
        startActivity(intent);
    }


}